今天會告訴你如何請求權限,之後教完新增鬧鐘頁面,會教你利用通知功能
需要先在AppDelegate中請求權限
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
說明:
func requestNotificationPermission() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
// 請求成功印出成功獲取
if granted {
print("Notification permission granted")
// 請求失敗印出獲取失敗
} else {
print("Notification permission denied")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
setUI()
setupNavigationBar()
requestNotificationPermission()
}